package graphic;

import java.awt.Color;
import java.awt.Font;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import utility.GraphicUtilsFactory;

import dbms.DBSelectingFunctions;

public class ReleasesListPanel extends JScrollPane {

	protected static ReleasesListPanel RELEASESPANEL;
	
	JTable releaseTable;
	DefaultTableModel releaseTableModel;
	
	HashMap<Integer, String> releaseNote;
	
	public ReleasesListPanel() {
	
		this.releaseNote = new HashMap<Integer, String>();
		
		this.createTable();
		
		super.getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
		super.setViewportView(this.releaseTable);
	}
	
	protected void createTable()
	{
		this.releaseTableModel = new DefaultTableModel()
		{ 
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		
		this.releaseTableModel.setColumnIdentifiers(ReleasesListPanel.getTableHeader());
		
		this.releaseTable = new JTable(this.releaseTableModel);
		this.releaseTable.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
		this.releaseTable.setForeground(Color.black);
		this.releaseTable.setGridColor(Color.black);
				
		this.releaseTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.releaseTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
		{	
			@Override
			public void valueChanged(ListSelectionEvent e)
			{
				int selectedRow = ReleasesListPanel.this.releaseTable.getSelectedRow();
				int releaseNumber = (Integer)ReleasesListPanel.this.releaseTable.getValueAt(selectedRow, 2);
				String note = ReleasesListPanel.this.releaseNote.get(releaseNumber);
				
				ReleaseNotePanel.getIstance().setReleaseNote(note);
			}
		});		
	}
	
	public void populateTable(int fileId)
	{
		try {
			this.createTable();
			
			this.releaseNote.clear();
			
			ResultSet ris = DBSelectingFunctions.getReleases(fileId);
			while(ris.next())
			{
				this.releaseTableModel.insertRow(this.releaseTable.getRowCount(), new Object[]{ris.getString("added_by"), ris.getTimestamp("added_when"), ris.getInt("number")});
				this.releaseNote.put(ris.getInt("number"), ris.getString("note"));
			}
			
			GraphicUtilsFactory.packTableColoumns(this.releaseTable, 2);
			
			super.setViewportView(this.releaseTable);
			super.revalidate();
			super.repaint();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	protected static Vector<String> getTableHeader()
	{
		Vector<String> header = new Vector<String>();
		header.add("Committed by");
		header.add("Committed in");
		header.add("Num");
		
		return header;
	}
	
	public static synchronized ReleasesListPanel getIstance()
	{
		if(RELEASESPANEL == null) RELEASESPANEL = new ReleasesListPanel();
		return RELEASESPANEL;
	}
	
}
